package com.yantong.pay.yfz.util;

import com.alibaba.fastjson.JSONObject;
import com.yantong.pay.yfz.domain.domain.Response;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class RemoteInvoker {
	private final static DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

	public static <T extends Response> T invoke(Object request, String url, String yplPrivateKey, String yplPrivateKeyPwd, String yplSignNo, String publicKeyFileUri, Class<T> c) throws Exception {
		String nowtime = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String jsonData = JSONObject.toJSONString(request);
		//签名
		String sign = RsaUtils.sign(KeyUtil.getPrimaryKey(yplPrivateKey, yplPrivateKeyPwd), jsonData);

		Map<String, String> header = new HashMap<String, String>(5);
		header.put("x-efps-sign", sign);
		header.put("x-efps-sign-no", yplSignNo);
		//header.put("x-efps-sign-no","562539003602372001");
		header.put("x-efps-sign-type", "SHA256withRSA");
		header.put("x-efps-timestamp", nowtime);
		header.put("Content-Type", "application/json");
		T response = HttpUtil.post(url, jsonData, header, c, publicKeyFileUri);

		return response;
	}
}
